package com.yenroc.ho.netty;

import com.dyuproject.protostuff.ProtobufIOUtil;
import com.dyuproject.protostuff.Schema;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * @author： heyanpeng
 * @date： 2021/7/28
 */
public class MessageDecoder extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        Schema<TkMessageVo> schema = RuntimeSchema.getSchema(TkMessageVo.class);
        TkMessageVo person = schema.newMessage();
        byte[] array = new byte[in.readableBytes()];
        in.readBytes(array);
        ProtobufIOUtil.mergeFrom(array, person, schema);
        out.add(person);
    }
}
